// pages/qesDetail/qesDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    question: {},

    fileCollection: [],
    filesDisplay: [], //显示的文件
    start: 0, //渲染开始位置
    skipNum: 5, //追加数据数量
    count: 0, //文件总数

    //筛选
    all: 1,
    myFaculty: 0,
    only_comment: 0,
    only_file: 0,

    //问题id
    qes_id: null, //页面跳转时获得

    followHidden: true,
    myqesHidden: false,

  },

  //评论点赞
  zan(e) {
    var that = this
    var index = e.currentTarget.id //数组下标
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/comment_like',
      method: "POST", //请求方式,
      header: {
        'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
      },
      data: {
        id: that.data.filesDisplay[index].id,
        zan: that.data.filesDisplay[index].zan,
        like_num: that.data.filesDisplay[index].like_num,
      },
      success(res) {
        console.log(res.data)
        var zan = that.data.filesDisplay[index].zan
        console.log(zan)
        //改变对象数组某一下标的对象值
        var change1 = "filesDisplay[" + index + "].zan"
        var change2 = "filesDisplay[" + index + "].like_num"
        if (zan) {
          wx.showToast({
            title: '点赞成功',
            icon: 'success',
            success() {
              that.setData({
                [change1]: !zan,
                [change2]: parseInt(that.data.filesDisplay[index].like_num) + 1
              })
            }
          })
        } else {
          wx.showToast({
            title: '取消成功',
            icon: 'success',
            success() {
              that.setData({
                [change1]: !zan,
                [change2]: parseInt(that.data.filesDisplay[index].like_num) - 1
              })
            }
          })
        }
      }
    })

  },

  //页面初始化
  onLoad: function (options) {
    var that = this
    var userInfo = getApp().globalData.userInfo //获取全局的用户信息
    wx.request({ //后端接口获取回答同一问题的文件和评论
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/get_qes_answer',
      method: "POST", //请求方式,
      header: {
        'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
      },
      data: {
        qes_id: options.qes_id,
        user_id: userInfo.user_id,
      },
      success(res) {
        if (userInfo.user_id == res.data.qes.user_id) {
          that.setData({
            myqesHidden: true,
          })
        }

        if (res.data.error_code == 0) {
          console.log(res.data)
          if (res.data.count > 100) {
            that.setData({
              fileCollection: res.data.data,
              count: "99+"
            })
          } else {
            that.setData({
              fileCollection: res.data.data,
              count: res.data.count
            })
          }
          that.setData({
            filesDisplay: that.data.fileCollection.slice(0, that.data.skipNum),
            start: that.data.skipNum,
            followHidden: !res.data.follow,
            qes_id: options.qes_id,
            question: res.data.qes,
          })
          console.log(that.data.filesDisplay)

        } else if (res.data.error_code == 2) {
          wx.showModal({
            title: '提示!',
            content: '该问题不存在',
            showCancel: false,
            success(res) {}
          })
        } else {
          console.log('qesDetail页面错误', res.data)
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '哎呀~!',
          content: '网络状态不佳呢！',
          showCancel: false,
          success(res) {}
        })
      }
    })

  },

  //触底刷新，获取更多
  onReachBottom: function () {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    var end = that.data.start + that.data.skipNum
    var newData = that.data.fileCollection.slice(that.data.start, end)
    that.setData({
      filesDisplay: that.data.filesDisplay.concat(newData), //拼接数组
      start: end
    })
    setTimeout(() => {
      wx.hideLoading()
    }, 500); //计时器  
  },

  //关注和取消关注
  follow() {
    var that = this
    var userInfo = getApp().globalData.userInfo //获取全局的用户信息
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/follow_or_not',
      method: "POST", //请求方式,
      header: {
        'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
      },
      data: {
        followHidden: that.data.followHidden,
        qes_id: that.data.qes_id,
        follow_id: userInfo.user_id, //不能为空，否则用户需先授权登录
      },
      success(res) {
        console.log(res.data)
        if (that.data.followHidden == true) {
          wx.showToast({
            title: '关注成功',
            icon: 'success',
          })
        } else {
          wx.showToast({
            title: '取消关注',
            icon: 'success',
            success(){
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1,
                })
              }, 1000);
              
            }
          })
        }
        that.setData({
          followHidden: !that.data.followHidden
        })

      }
    })
  },

  //筛选条件
  //全部
  All: function () {
    var that = this
    var userInfo = getApp().globalData.userInfo //获取全局的用户信息
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({ //后端接口未完成，获取回答同一问题的文件和评论
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/get_qes_answer',
      method: "POST", //请求方式,
      header: {
        'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
      },
      data: {
        qes_id: that.data.qes_id,
        user_id: userInfo.user_id,
      },
      success(res) {
        console.log(res.data)
        if (res.data.count > 100) {
          that.setData({
            fileCollection: res.data.data,
            count: "99+"
          })
        } else {
          that.setData({
            fileCollection: res.data.data,
            count: res.data.count,
          })
        }
        that.setData({
          //筛选
          all: 1,
          myFaculty: 0,
          only_comment: 0,
          only_file: 0,
          filesDisplay: that.data.fileCollection.slice(0, that.data.skipNum),
          start: that.data.skipNum
        })
        wx.hideLoading()
      }
    })
  },

  //本学校
  Faculty: function () {
    var that = this
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({ //后端接口获取本学院回答同一问题的文件和评论
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/get_mySchool_answer',
      method: "POST", //请求方式,
      header: {
        'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
      },
      data: {
        user_school: getApp().globalData.userInfo.user_school,
        qes_id: that.data.qes_id,
      },
      success(res) {
        console.log(res.data)
        if (res.data.count > 100) {
          that.setData({
            fileCollection: res.data.data,
            count: "99+"
          })
        } else {
          that.setData({
            fileCollection: res.data.data,
            count: res.data.count,
          })
        }
        that.setData({
          //筛选
          all: 0,
          myFaculty: 1,
          only_comment: 0,
          only_file: 0,
          filesDisplay: that.data.fileCollection.slice(0, that.data.skipNum),
          start: that.data.skipNum
        })
        wx.hideLoading()
      }
    })
  },

  //仅评论
  comment: function () {
    var that = this
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({ //后端接口获取回答同一问题的评论
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/get_only_comment',
      method: "POST", //请求方式,
      header: {
        'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
      },
      data: {
        qes_id: that.data.qes_id,
      },
      success(res) {
        console.log(res.data)
        if (res.data.count > 100) {
          that.setData({
            fileCollection: res.data.data,
            count: "99+"
          })
        } else {
          that.setData({
            fileCollection: res.data.data,
            count: res.data.count,
          })
        }
        that.setData({
          //筛选
          all: 0,
          myFaculty: 0,
          only_comment: 1,
          only_file: 0,
          filesDisplay: that.data.fileCollection.slice(0, that.data.skipNum),
          start: that.data.skipNum
        })
        wx.hideLoading()
      }
    })
  },

  //仅文件
  onlyFile: function () {
    var that = this
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({ //后端接口获取回答同一问题的文件
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/get_only_files',
      method: "POST", //请求方式,
      header: {
        'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
      },
      data: {
        qes_id: that.data.qes_id,
      },
      success(res) {
        console.log(res.data)
        if (res.data.count > 100) {
          that.setData({
            fileCollection: res.data.data,
            count: "99+"
          })
        } else {
          that.setData({
            fileCollection: res.data.data,
            count: res.data.count,
          })
        }
        that.setData({
          //筛选
          all: 0,
          myFaculty: 0,
          only_comment: 0,
          only_file: 1,
          filesDisplay: that.data.fileCollection.slice(0, that.data.skipNum),
          start: that.data.skipNum
        })
        wx.hideLoading()
      }
    })
  },

  //删除文件
  deleteQes() {
    var that = this
    wx.showModal({
      title: '温馨提示',
      content: '确认删除？',
      showCancel: true,
      success(res) {
        if (res.confirm) {
          wx.request({
            url: 'https://www.cerywxr.com/treehole/index.php/Home/ques/delete_one',
            method: "POST", //请求方式,
            header: {
              'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
            },
            data: {
              qes_id: that.data.qes_id,
            },
            success(res) {
              console.log(res.data)
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                success(){
                  setTimeout(() => {
                    wx.navigateBack({
                      delta: 1,
                    })
                  }, 1000);
                }
              })
            }
          })
        }
      }
    })

  }

})